/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.FeedbackDAO;
import Models.FeedBack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Admin
 */
@ManagedBean
@RequestScoped
public class FeedBackController {

    ArrayList<FeedBack> listFeedback;

    public FeedBackController() {
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public String processSubmit() {
        FeedBack fb = (FeedBack) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("feedbackBean"));
        new FeedbackDAO().insertFeedBack(fb);
        return "FeedBackDone";
    }

    public ArrayList<FeedBack> getListFeedback() {
        listFeedback = new FeedbackDAO().getAllFeedback();
        return listFeedback;
    }

    public void deleteFeedback() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        new FeedbackDAO().deleteFeedbackByID(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Feedback has been deleted !"));
    }
}
